package com.winning.sjzc.zyml.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winning.sjzc.common.utils.MathUtils;
import com.winning.sjzc.zyml.dao.PsmsSjjxxMapper;
import com.winning.sjzc.zyml.entity.PsmsSjjxxEntity;
import com.winning.sjzc.zyml.entity.vo.SjjywxxEntity;
import com.winning.sjzc.zyml.service.PsmsSjjxxService;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * description:数据集信息
 *
 * @author yanxf@winning.com.cn
 * @date 2019/08/14 14:55
 */
@Service
public class PsmsSjjxxServiceImpl implements PsmsSjjxxService {

  @Autowired
  private PsmsSjjxxMapper psmsSjjxxMapper;

  @Override
  public List<SjjywxxEntity> findByFldm(String sjjfldm) {
    return psmsSjjxxMapper.querySjjywxx(sjjfldm);
  }

  @Override
  public IPage<PsmsSjjxxEntity> queryPage(Page<PsmsSjjxxEntity> page, String sjjdm) {
    return psmsSjjxxMapper.selectPage(page, new QueryWrapper<PsmsSjjxxEntity>()
        .eq("SJJFLDM", sjjdm));
  }

  @Override
  public Map<String, Object> querySjjNum(String sjjfldm) {
    Map<String, Object> map = psmsSjjxxMapper.querySjjNum(sjjfldm);
    Integer brl = getInt(map, "brl");
    BigDecimal brlDecimal = MathUtils.divide(new BigDecimal(brl), new BigDecimal(1024 * 1024));
    Integer jls = getInt(map, "jls");
    BigDecimal jlsDecimal = MathUtils.divide(new BigDecimal(jls), new BigDecimal(10000));
    map.put("brl", brlDecimal);
    map.put("jls", jlsDecimal);
    return map;
  }

  private Integer getInt(Map<String, Object> map, String key) {
    Object o = map.get(key);
    if (o == null) {
      return 0;
    }
    if (o instanceof Integer){
      return (Integer) o;
    } else if (o instanceof BigDecimal) {
      BigDecimal sum = (BigDecimal )o;
      return sum.intValue();
    }
    return 0;
  }

  @Override
  public List<PsmsSjjxxEntity> findAll() {
    return psmsSjjxxMapper.selectList(null);
  }
}
